package leecode

import (
	"fmt"
	"strconv"
)

func sumNumbersLCR(root *TreeNode) int {
	nums := make([]byte, 0)
	nums = append(nums, byte(root.Val+48))
	numStr := make([]string, 0)
	var backtracking func(node *TreeNode)
	backtracking = func(node *TreeNode) {
		if node.Left == nil && node.Right == nil {
			temp := make([]byte, len(nums))
			copy(temp, nums)
			fmt.Println(temp)
			str := string(temp)
			fmt.Println("ee", str)
			numStr = append(numStr, str)
			return
		}
		if node.Left != nil {
			nums = append(nums, byte(node.Left.Val+48))
			backtracking(node.Left)
			nums = nums[:len(nums)-1]
		}
		if node.Right != nil {
			nums = append(nums, byte(node.Right.Val+48))
			backtracking(node.Right)
			nums = nums[:len(nums)-1]
		}
	}

	backtracking(root)
	var result int
	for _, num := range numStr {
		temp, _ := strconv.Atoi(num)
		result += temp
	}
	return result
}
